package com.mes.basicdata.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.bstek.dorado.data.provider.Page;
import com.mes.basicdata.dao.iface.IOperationDao;
import com.mes.basicdata.dao.iface.IRoutingDao;
import com.mes.basicdata.domain.Employee;
import com.mes.basicdata.domain.Operation;
import com.mes.basicdata.domain.Routing;
import com.mes.basicdata.domain.RoutingOperation;
import com.mes.common.dao.BasicDao;

@Repository
public class OperationDao extends BasicDao<Operation> implements IOperationDao {

	@Autowired
	IRoutingDao routingDao;

	/**
	 * 按照多个Operation对象属性查询Operation列表
	 * @param page
	 * @param params
	 * @throws Exception
	 */
	@Override
	public void getOperationsByConditions(Page<Operation> page, Map<String, Object> params) throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> args = new HashMap<String, Object>();
		StringBuffer coreHql = new StringBuffer("from " + Operation.class.getName() + " a where 1=1 ");

		String operationIdDesc = (String) params.get("operationIdDesc");
		if (StringUtils.isNotEmpty(operationIdDesc)) {
			coreHql.append(" and a.operationIdDesc like :operationIdDesc ");
			args.put("operationIdDesc", "%" + operationIdDesc + "%");
		}
		String operationName = (String) params.get("operationName");
		if (StringUtils.isNotEmpty(operationName)) {
			coreHql.append(" and a.operationName like :operationName ");
			args.put("operationName", "%" + operationName + "%");
		}

		String countHql = "select count(*) " + coreHql.toString();
		String hql = coreHql.toString();

		Map<String, Object> newArgs = new HashMap<String, Object>();
		String key;
		String newKey;
		Iterator<String> iterator = args.keySet().iterator();
		while (iterator.hasNext()) {
			key = (String) iterator.next();
			newKey = key.replace(".", "_");
			newArgs.put(newKey, args.get(key));
			hql = hql.replace(":" + key, ":" + newKey);
			countHql = countHql.replace(":" + key, ":" + newKey);

			System.out.println("hql: " + hql);
			System.out.println("countHql: " + countHql);
			System.out.println("newArgs: " + newArgs);
		}
		this.pagingQuery(page, hql, countHql, newArgs);
	}

	/**
	 * 获得某个工艺下的所有工序列表，并按照operationId正向排序
	 * @param routingUid 工艺的唯一号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Operation> getOperationListByRoutingUid(String routingUid) throws Exception {
		Routing routing = routingDao.getById(routingUid);
		Set<RoutingOperation> routingOperations = routing.getRoutingOperations();
		List<Operation> operations = new ArrayList<Operation>();
		for (RoutingOperation ro : routingOperations) {
			Operation operation = ro.getOperation();
			operations.add(operation);
		}
		return operations;
	}

	/**
	 * 获得某个工艺下的所有工序列表，并按照operationId正向排序
	 * @param routingUid 工艺的唯一号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Operation> getOperations(String routingUid) throws Exception {
		Session session = this.getSessionFactory().getCurrentSession();
		List<Operation> operations = new ArrayList<Operation>();
		List<RoutingOperation> routineOperations = null;
		try {
			StringBuffer hql = new StringBuffer("from " + RoutingOperation.class.getName() + " where ");
			hql.append("routing = ? order by operationId");
			Query query = session.createQuery(hql.toString());
			query.setString(0, routingUid);
			routineOperations = query.list();
			for (RoutingOperation routineOperation : routineOperations) {
				Operation operation = this.getById(routineOperation.getOperationUid());
				operations.add(operation);
			}
			return operations;
		} finally {
			session.flush();
		}
	}

	/**
	 * 查询某个工序的工步列表。【查询出来的工步没有排序，待修改】
	 * @param params
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<Operation> getOperationStepByParentOpUid(Map<String, Object> params) throws Exception {
		// TODO 此处查询出来的工步是未经排序的，待修改
		String parentOpUid = (String) params.get("parentOpUid");
		return this.findByProperty("parentOpUid", parentOpUid);
	}

}
